//
//  CWNetworkTool.swift
//  CWWeibo06
//
//  Created by ycw on 2017/7/23.
//  Copyright © 2017年 xiaowei. All rights reserved.
//

import UIKit
import AFNetworking

enum responseType {
    case get
    case post
}

class CWNetworkTool: AFHTTPSessionManager {
    
    static let sharedTools: CWNetworkTool = {
        
        let tools = CWNetworkTool()
        
        tools.responseSerializer.acceptableContentTypes?.insert("text/html")
        
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        
        return tools
    }()

    open func loadNetworkData(type: responseType,urlString: String,params: Any?,callBack: @escaping (Any?,Error?)->()) {
        
        if type == .get {
            
            get(urlString, parameters: params, progress: nil, success: { (_, responseObject) in
                callBack(responseObject, nil)
                print(responseObject!)
            }, failure: { (_, error) in
                callBack(nil, error)
                print(error)
            })
            
        }else {
            
            post(urlString, parameters: params, progress: nil, success: { (_, responseObject) in
                callBack(responseObject, nil)
                print(responseObject!)
            }, failure: { (_, error) in
                callBack(nil, error)
                print(error)
            })
        }
        
    }
    

}


//MARK: OAuth相关的接口
extension CWNetworkTool {
    
    //请求授权令牌
    open func requestAccessToken(callBack: @escaping (Any?, Error?) -> ()) {
        
        let urlString: String = "https://api.weibo.com/oauth2/access_token"
        
        let params: [String : Any] = ["client_id":AppKey,
                                  "client_secret":"30da8672b4b8ef5d314ccbcae2dc342f",
                                     "grant_type":"authorization_code",
                                           "code":code,
                                   "redirect_uri":redirect_uri]
        
        loadNetworkData(type: .post, urlString: urlString, params: params, callBack: callBack)
        
    }
    
    //请求用户信息
    open func requestUserInfo(access_token: String,uid: Int64,callBack: @escaping (Any?, Error?) -> ()) {
        
        let urlString: String = "https://api.weibo.com/2/users/show.json"
        
        let params: [String:Any] = ["access_token":access_token,"uid":uid]
        
        loadNetworkData(type: .get, urlString: urlString, params: params, callBack: callBack)
        
    }
    
}

//MARK: 首页相关的接口

extension CWNetworkTool {
    
    /// 请求用户登录及其所关注的微博数据
    ///
    /// - Parameters:
    ///   - accessToken: 授权令牌
    ///   - maxID: 返回ID小于或等于max_id的微博，默认为0
    ///   - sinceID: 返回ID比since_id大的微博（即比since_id时间晚的微博），默认为0
    ///   - callBack: 完成后的回调
    open func requestStatuses(accessToken: String, maxID: Int64 = 0, sinceID: Int64 = 0, callBack: @escaping (Any?, Error?) -> ()) {
        
        let urlString: String = "https://api.weibo.com/2/statuses/home_timeline.json"
        
        let params: [String:Any] = ["access_token":accessToken,
                                    "max_id":maxID,
                                    "since_id":sinceID]
        
        CWNetworkTool.sharedTools.loadNetworkData(type: .get, urlString: urlString, params: params, callBack: callBack)
        
    }
    
}








